package com.ruoyi.population.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.population.domain.xlkjPopSurvey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.population.mapper.XlkjPopulationMapper;
import com.ruoyi.population.domain.XlkjPopulation;
import com.ruoyi.population.service.IXlkjPopulationService;
import com.ruoyi.common.exception.CustomException;

/**
 * 人口Service业务层处理
 *
 * @author wang
 * @date 2022-01-11
 */
@Service
public class XlkjPopulationServiceImpl implements IXlkjPopulationService
{
    @Autowired
    private XlkjPopulationMapper xlkjPopulationMapper;

    /**
     * 查询人口
     *
     * @param id 人口主键
     * @return 人口
     */
    @Override
    public XlkjPopulation selectXlkjPopulationById(Long id)
    {
        return xlkjPopulationMapper.selectXlkjPopulationById(id);
    }

    /**
     * 查询人口列表
     *
     * @param xlkjPopulation 人口
     * @return 人口
     */
    @Override
    public List<XlkjPopulation> selectXlkjPopulationList(XlkjPopulation xlkjPopulation)
    {
        return xlkjPopulationMapper.selectXlkjPopulationList(xlkjPopulation);
    }

    /**
     * 新增人口
     *
     * @param xlkjPopulation 人口
     * @return 结果
     */
    @Override
    public int insertXlkjPopulation(XlkjPopulation xlkjPopulation)
    {
        xlkjPopulation.setCreateTime(DateUtils.getNowDate());
        return xlkjPopulationMapper.insertXlkjPopulation(xlkjPopulation);
    }

    /**
     * 修改人口
     *
     * @param xlkjPopulation 人口
     * @return 结果
     */
    @Override
    public int updateXlkjPopulation(XlkjPopulation xlkjPopulation)
    {
        xlkjPopulation.setUpdateTime(DateUtils.getNowDate());
        return xlkjPopulationMapper.updateXlkjPopulation(xlkjPopulation);
    }

    /**
     * 批量删除人口
     *
     * @param ids 需要删除的人口主键
     * @return 结果
     */
    @Override
    public int deleteXlkjPopulationByIds(Long[] ids)
    {
        return xlkjPopulationMapper.deleteXlkjPopulationByIds(ids);
    }

    /**
     * 删除人口信息
     *
     * @param id 人口主键
     * @return 结果
     */
    @Override
    public int deleteXlkjPopulationById(Long id)
    {
        return xlkjPopulationMapper.deleteXlkjPopulationById(id);
    }

    /**
     * 人口数据
     *
     * @param details
     */
    @Override
    public String importDevice(List<XlkjPopulation> details) {
        if (StringUtils.isNull(details) || details.size() == 0){
            throw new CustomException("导入的人口数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (XlkjPopulation user : details){
            try{
                this.insertXlkjPopulation(user);
                successNum++;
                successMsg.append("导入成功");
            }catch (Exception e){
                failureNum++;
                String msg = "导入失败";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0){
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else{
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public List<XlkjPopulation> selectPopByDepartmentId(String[] departmentId) {
        return xlkjPopulationMapper.selectPopByDepartmentId(departmentId);
    }

    @Override
    public xlkjPopSurvey selectPeopleNumByDeptId(Long deptId) {
        return xlkjPopulationMapper.selectPeopleNumByDeptId(deptId);
    }


}
